package com.sangmo.fts.common.excel.impl;

import com.sangmo.boot.framework.data.repo.IRepo;
import com.sangmo.fts.common.excel.IPageDataProvider;
import com.sangmo.boot.framework.common.utils.ApplicationUtils;
import com.sangmo.boot.framework.common.utils.PropertyCopyUtil;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.PageSearch;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class DefaultPageDataProvider implements IPageDataProvider {
    private boolean end;
    private int page;
    private int size = 500;
    private PageSearch pageSearch;
    private Class entityClass;
    private Class modelClass;
    private DefaultPageDataProvider.IPageQuery pageQuery;

    public DefaultPageDataProvider(PageSearch pageSearch, final Class entityClass, Class modelClass) {
        this.pageSearch = pageSearch;
        this.entityClass = entityClass;
        this.modelClass = modelClass;
        this.pageQuery = new DefaultPageDataProvider.IPageQuery() {
            public PageResult search(PageSearch pageSearch) {
                IRepo repo = (IRepo)ApplicationUtils.getBean(IRepo.class, entityClass);
                return repo.search(pageSearch);
            }
        };
    }

    public DefaultPageDataProvider(PageSearch pageSearch, Class entityClass, Class modelClass, DefaultPageDataProvider.IPageQuery pageQuery) {
        this.pageSearch = pageSearch;
        this.entityClass = entityClass;
        this.modelClass = modelClass;
        this.pageQuery = pageQuery;
    }

    public PageSearch getPageSearch() {
        return this.pageSearch;
    }

    public void setPageSearch(PageSearch pageSearch) {
        this.pageSearch = pageSearch;
    }

    public Class getEntityClass() {
        return this.entityClass;
    }

    public void setEntityClass(Class entityClass) {
        this.entityClass = entityClass;
    }

    public Class getModelClass() {
        return this.modelClass;
    }

    public void setModelClass(Class modelClass) {
        this.modelClass = modelClass;
    }

    public List next() {
        List list = new ArrayList();
        if (!this.end) {
            this.pageSearch.setPage(this.page++);
            this.pageSearch.setSize(this.size);
            PageResult result = this.pageQuery.search(this.pageSearch);

            try {
                if (this.modelClass.getName().equals(this.entityClass.getName())) {
                    list = result.getRows();
                }

                Iterator var3 = result.getRows().iterator();

                while(var3.hasNext()) {
                    Object obj = var3.next();
                    Object view = this.modelClass.newInstance();
                    PropertyCopyUtil.getInstance().copyProperties(view, obj);
                    ((List)list).add(view);
                }

                if (result.getTotal() <= (long)((result.getPage() + 1) * result.getSize())) {
                    this.end = true;
                }
            } catch (Exception var6) {
                throw new RuntimeException(var6);
            }
        }

        return (List)list;
    }

    public interface IPageQuery {
        PageResult search(PageSearch var1);
    }
}
